﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPMVCUtil.Helpers;

namespace System.Web.Mvc.Html
{
    public enum TablaVisualizadaTipos 
    {
        bar,
        pie,
        line,
        area
    }
    public static class TablaVisualizadaHelper
    {
        public static MvcHtmlString ponerTablaVisualizada(this HtmlHelper helper, string id, string titulo, TablaVisualizadaTipos tipo,
            int ancho, int alto, string[][] datos, params string[] colores)
        {
            return MvcHtmlString.Create(grafica(id, datos, titulo) + JS.outl() + tabla(id, datos, titulo) + JS.outl() + 
                script(id, tipo, ancho, alto, colores));
        }

        private static string grafica(string id, string[][] datos, string titulo)
        {
            TagBuilder tabla = new TagBuilder("table");
            tabla.MergeAttribute("id", id);
            tabla.MergeAttribute("style", "display: none");

            TagBuilder caption = new TagBuilder("caption");
            caption.InnerHtml = titulo;

            tabla.InnerHtml = caption.ToString(TagRenderMode.Normal) + JS.outl() + contenidoTabla(datos);

            return tabla.ToString(TagRenderMode.Normal);
        }

        private static string tabla(string id, string[][] datos, string titulo)
        {
            TagBuilder tabla = new TagBuilder("table");
            tabla.MergeAttribute("id", id + "_dialog");
            tabla.MergeAttribute("title", titulo);
            tabla.MergeAttribute("class", "tablaVisualizada_dialogo");
            tabla.InnerHtml = contenidoTablaTotales(datos);                

            TagBuilder button = new TagBuilder("button");
            button.MergeAttribute("id", id + "_opener");
            button.MergeAttribute("class", "botong");
            button.MergeAttribute("style", "margin-top: 5px");
            button.MergeAttribute("title", "Muestra la tabla abajo de este botón, pero usted puede moverla en la pantalla");
            button.InnerHtml = "Ver tabla";

            return tabla.ToString(TagRenderMode.Normal) + JS.outl() + button.ToString(TagRenderMode.Normal);
        }

        private static string contenidoTabla(string[][] datos)
        {            
            TagBuilder thead = new TagBuilder("thead");

            TagBuilder tr = new TagBuilder("tr");

            TagBuilder td = new TagBuilder("td");

            tr.InnerHtml = td.ToString(TagRenderMode.Normal);

            for(int i = 1; i < datos[0].Length; i++) 
            {
                TagBuilder th = new TagBuilder("th");
                th.InnerHtml = datos[0][i];
                tr.InnerHtml += th.ToString(TagRenderMode.Normal);
            }
            
            thead.InnerHtml = tr.ToString(TagRenderMode.Normal);

            TagBuilder tbody = new TagBuilder("tbody");

            for(int i = 1; i < datos.Length; i++) 
            {
                TagBuilder tr1 = new TagBuilder("tr");

                TagBuilder th1 = new TagBuilder("th");
                th1.MergeAttribute("scope", "row");
                th1.InnerHtml = datos[i][0];

                tr1.InnerHtml = th1.ToString(TagRenderMode.Normal);
                
                for(int j = 1; j < datos[i].Length; j++) 
                {
                    TagBuilder td1 = new TagBuilder("td");
                    td1.InnerHtml = datos[i][j];

                    tr1.InnerHtml += td1.ToString(TagRenderMode.Normal);
                }

                tbody.InnerHtml += tr1.ToString(TagRenderMode.Normal);
            }
            return thead.ToString(TagRenderMode.Normal) + JS.outl() + tbody.ToString(TagRenderMode.Normal);
        }

        private static string contenidoTablaTotales(string[][] datos)
        {
            TagBuilder thead = new TagBuilder("thead");

            TagBuilder tr = new TagBuilder("tr");

            TagBuilder td = new TagBuilder("td");

            tr.InnerHtml = td.ToString(TagRenderMode.Normal);

            for (int i = 1; i < datos[0].Length; i++)
            {
                TagBuilder th = new TagBuilder("th");
                th.InnerHtml = datos[0][i];
                tr.InnerHtml += th.ToString(TagRenderMode.Normal);
            }

            if (datos[0].Length > 2)
            {
                TagBuilder thTotales = new TagBuilder("th");
                thTotales.MergeAttribute("style", "background-color: #A9E32C");
                thTotales.InnerHtml = "Total";
                tr.InnerHtml += thTotales.ToString(TagRenderMode.Normal);
            }

            thead.InnerHtml = tr.ToString(TagRenderMode.Normal);

            TagBuilder tbody = new TagBuilder("tbody");

            for (int i = 1; i < datos.Length; i++)
            {
                TagBuilder tr1 = new TagBuilder("tr");

                TagBuilder th1 = new TagBuilder("th");
                th1.MergeAttribute("scope", "row");
                th1.InnerHtml = datos[i][0];

                tr1.InnerHtml = th1.ToString(TagRenderMode.Normal);

                double totalFila = 0;

                for (int j = 1; j < datos[i].Length; j++)
                {
                    TagBuilder td1 = new TagBuilder("td");
                    td1.InnerHtml = datos[i][j];
                    totalFila += Convert.ToDouble(datos[i][j]);

                    tr1.InnerHtml += td1.ToString(TagRenderMode.Normal);
                }

                if (datos[0].Length > 2)
                {
                    TagBuilder tdTotales = new TagBuilder("td");
                    tdTotales.InnerHtml = totalFila + "";
                    tdTotales.MergeAttribute("style", "background-color: #A9E32C");

                    tr1.InnerHtml += tdTotales.ToString(TagRenderMode.Normal);
                }

                tbody.InnerHtml += tr1.ToString(TagRenderMode.Normal);
            }
            return thead.ToString(TagRenderMode.Normal) + JS.outl() + tbody.ToString(TagRenderMode.Normal);
        }

        private static string script(string id, TablaVisualizadaTipos tipo, int ancho, int alto, params string[] colores) 
        {            
            TagBuilder script = new TagBuilder("script");
            script.MergeAttribute("type", "text/javascript");
            string js = JS.outl("   $(function () {");
            js += JS.outl("        $('#" + id + "_dialog').dialog({");
            js += JS.outl("            autoOpen: false,");
            js += JS.outl("            show: 'highlight',");
            js += JS.outl("            hide: 'explod',");
            js += JS.outl("            modal: false,");
            js += JS.outl("            width: 'auto'");
            js += JS.outl("        });");
            js += JS.outl("");
            js += JS.outl("        $('#" + id + "_opener').click(function () {");
            js += JS.outl("            $('#" + id + "_dialog').dialog('open');");
            js += JS.outl("            $('#" + id + "_dialog').dialog('widget').position({");
            js += JS.outl("               my: 'left top',");
            js += JS.outl("               at: 'left bottom',");
            js += JS.outl("               of: $(this)");
            js += JS.outl("            });");
            js += JS.outl("            return false;");
            js += JS.outl("        });");
            js += JS.outl("");
            js += JS.outl("        $('#" + id + "').visualize({ type: '" + tipo.ToString() + "', height: '" + alto + "px', width: '" + ancho + "px', pieMargin: 10,");
            string cls = "";
            int i = 0;
            foreach (string color in colores)
            {
                if(i == 0)
                    cls += "'" + color + "'";
                else
                    cls += ", '" + color + "'";
                i++;
            }
            js += JS.outl("            colors: [" + cls + "]");
            js += JS.outl("        });");
            js += JS.outl("    });");
            script.InnerHtml = js;
            return script.ToString(TagRenderMode.Normal);
        }
    }
}